using System.Diagnostics;
using System;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Data;
using System.Collections.Generic;
using System.Xml;
using MSXML2;

namespace FanTong
{
	sealed class XMLMethod
	{
		
		//发送XMLHTTP请求，带返回值
        public static DOMDocumentClass sendRequest(string Url, bool UseAuth, string userName, string password, ref DateTime ServerTime, ref string ErrorMsg)
		{
            XMLHTTPClass xmlhttp = new XMLHTTPClass();
            
            //System.Object xmlhttp = Interaction.CreateObject("Microsoft.XMLHTTP", "");
			DOMDocumentClass xmldom = new MSXML2.DOMDocumentClass();
            var xmls = string.Empty;
			try
			{
				if (UseAuth)
				{
					xmlhttp.open("POST", Url, false, userName, password);
				}
				else
				{
                    xmlhttp.open("POST", Url, false,null,null);
				}
				
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                xmlhttp.send(xmls);
				
				xmldom.async = false;
				xmldom.loadXML(xmlhttp.responseText);
				
				//ReadState 4
				//MsgBox(xmlhttp.readyState)
				//Status 200ok, 401error
				//MsgBox(xmlhttp.Status)
				
				//每次取得最新服务器时间
				object sServerTime = xmlhttp.getResponseHeader("Date");
                //System.String Year = DateTime.Now.Year.ToString(); // Strings.Mid(sServerTime, 13, 4);
                //System.String Month = DateTime.Now.Month.ToString(); // Strings.Mid(sServerTime, 9, 3);
                //System.String Day = DateTime.Now.Day.ToString(); //Strings.Mid(sServerTime, 6, 2);
                //System.String time = DateTime.Now.time.ToString(); //Strings.Mid(sServerTime, 18, 8);
                ServerTime = DateTime.Now;
				ServerTime = ServerTime.AddHours(8);
			}
			catch (Exception ex)
			{
				ErrorMsg = ex.Message;
			}
			
			return xmldom;
			
		}
	}
	
}
